﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Cosmetic.Logic
{
    /// <summary>
    /// xuser manager class
    /// </summary>
    public class xUserManager : LogicBase
    {

        /// <summary>
        /// Insert a new User into database
        /// </summary>
        /// <param name="aUser">new User</param>
        public void Insert(xuser aUser)
        {
            m_CosmeticDC.xuser.InsertOnSubmit(aUser);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// Delete User
        /// </summary>
        /// <param name="aUser">The User want delete</param>
        public void Delete(xuser aUser)
        {
            m_CosmeticDC.xuser.DeleteOnSubmit(aUser);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 获取用户的手机号码获取用户信息
        /// </summary>
        /// <param name="aUser">new User</param>
        public List<xuser> GetAlles(string a)
        {
            return (from theActivity in m_CosmeticDC.xuser
                    where theActivity.tel == a
                    orderby theActivity.time descending
                    select theActivity).ToList();
        }
        public xuser GetUserById(Guid aUserId)
        {
            List<xuser> theUserList = (from theUser in m_CosmeticDC.xuser 
                                      where theUser.id  == aUserId
                                      select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }

        public xuser GetUserByIds(Guid aUserId)
        {
            List<xuser> theUserList = (from theUser in m_CosmeticDC.xuser
                                       where theUser.id == aUserId
                                       select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }

        /// <summary>
        /// 登录时判断是否有次人
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="aPassword"></param>
        /// <returns></returns>
        public xuser LoginValidate(string aName, string aPassword)
        {
            List<xuser> theUserList = (from theUser in m_CosmeticDC.xuser
                                      where theUser.name  == aName && theUser.mima  == aPassword
                                      select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }

        public xuser GetUserByEmail(string anEmail)
        {
            List<xuser> theUserList = (from theUser in m_CosmeticDC.xuser
                                      where theUser.email  == anEmail
                                      select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }
        public xuser GetUserByname(string anEmail)
        {
            List<xuser> theUserList = (from theUser in m_CosmeticDC.xuser 
                                       where theUser.name  == anEmail
                                       select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }


        public List<DiscountCard> GetDiscountCardByUserId(Guid UserId)
        {
            var result = (from dis in m_CosmeticDC.DiscountCards
                          where dis.UserId == UserId
                          select dis).ToList();
            return result;
        }
        public DiscountCard GetDiscountCardById(string Id)
        {
            DiscountCard result = (from dis in m_CosmeticDC.DiscountCards
                                   where dis.Id == Id
                                   select dis).Single();
            return result;
        }

        public decimal GetAccConsume(Guid UserId)
        {
            List<Order> result = (from ord in m_CosmeticDC.Orders
                                  where ord.UserId == UserId
                                  select ord).ToList();
            decimal SumConsume = 0;

            for (int i = 0; i < result.Count; i++)
            {
                SumConsume += result[i].Price.Value;
            }

            return SumConsume;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public decimal GetAccBack(Guid UserId)
        {
            List<Order> result = (from ord in m_CosmeticDC.Orders
                                  where ord.UserId == UserId && ord.State == "取消"
                                  select ord).ToList();

            decimal SumConsume = 0;

            for (int i = 0; i < result.Count; i++)
            {
                SumConsume += result[i].Price.Value;
            }
            return SumConsume;
        }
        /// <summary>
        /// 获取用户的全部信息
        /// </summary>
        /// <param name="aUser">new User</param>
        public List<xuser > GetAlles1()
        {
            return (from theActivity in m_CosmeticDC.xuser 
                    orderby theActivity.time  descending
                    select theActivity).ToList();
        }

        public IEnumerable GetAllnumbers()
        {
            var result = from theFocus in m_CosmeticDC.xuser 
                         join thexuser in m_CosmeticDC .xuser 
                         on  theFocus .tuserid  equals thexuser .id 
                         select new
                         {
                             id = theFocus.id,
                             name=theFocus .name ,
                             email=theFocus .email ,
                             mima=theFocus .mima ,
                             jibie=theFocus .jibie ,
                             tel=theFocus .tel ,
                             type=theFocus .type ,
                             time=theFocus .time ,
                             vlause=theFocus .vlause ,
                             tname=thexuser .name 

                         };
            return result;
        }






         public static void UpdateInformations(Guid id, string bb)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "UPDATE [xuser]  SET [jibie]='" + bb + "'  WHERE id='" + id + "'";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }

        public List<xuser> GetAlles10( Guid userid)
        {
            return (from theActivity in m_CosmeticDC.xuser where theActivity.tuserid == userid 
                    orderby theActivity.time descending
                    select theActivity).ToList();
        }



    }
}
